#include<stdio.h>
#include<stdlib.h>

int count=0;

int *create();
void display(int *);
int *append(int *);
int *extend(int *);
void max(int *);
void min(int *);
int *sort(int *);
int *reverse(int *);
int *delete(int *);
int *pop(int *);
void index(int *);
void ccount(int *);
int *insert(int *);

void main()
{
int *a;
int ch;
a=NULL;

do
{
printf("Enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1 :
{
a=create();
break;
}
case 2:
{
a=insert(a);
break;
}
case 3: 
{
a=append(a);
break;
}
case 4:
{
a=extend(a);
break;
}
case 5:
{
max(a);
break;
}
case 6:
{
min(a);
break;
}
case 7:
{
ccount(a);
break;
}
case 8:
{
index(a);
break;
}
case 9:
{
delete(a);
break;
}
case 10:
{
pop(a);
break;
}
case 11:
{
a=reverse(a);
break;
}
case 12:
{
a=sort(a);
break;
}
case 13:
{
display(a);
break;
}
case 14:
{
break;
}
default:
printf("You entered a wrong choice");
}
}
while(ch!=14);
/*a=create(a);
display(a);
a=append(a);
display(a);
a=extend(a);
display(a);
max(a);
min(a);
a=sort(a);
display(a);
a=reverse(a);
display(a);
a=delete(a);
display(a);
a=pop(a);
display(a);
index(a);
ccount(a);
a=insert(a);
display(a);
*/
}

int *create()
{
int *b;
int n,i,num;
printf("Enter the number of elements you want to enter in the list : ");
scanf("%d",&n);
b=(int *)malloc(n*(sizeof(int *)));
for(i=0;i<n;i++)
{
printf("Enter the element : ");
scanf("%d",&num);
*(b+i)=num;
}
count=n;
return b;
}

void display(int *list)
{
int i;
printf("[");
for(i=0;i<count;i++)
{
printf(" %d ",*(list+i));
}
printf("]\n");
}

int *append(int *list)
{
int num,i;
int *b;
b=list;
list=(int *)malloc(count*(sizeof(int *)));
printf("Enter the number you want to add into the list : ");
scanf("%d",&num);
for(i=0;i<count;i++)
{
*(list+i)=*(b+i);
}
*(list+count)=num;
count++;
return list;
}

void max(int *list)
{
int max;
int i;
max=*list;
for(i=0;i<count;i++)
{
if(max<(*(list+i)))
{
max=(*(list+i));
}
}
printf("The maximum number in the list is : %d\n ",max);
}

void min(int *list)
{
int min,i;
min=(*list);
for(i=0;i<count;i++)
{
if(min>(*(list+i)))
{
min=(*(list+i));
}
}
printf("The minimum element in the list is : %d\n",min);
}

int *sort(int *list)
{
int i,t,j;
for(i=0;i<count;i++)
{
for(j=0;j<count;j++)
{
if(*(list+i)<*(list+j))
{
t=*(list+i);
*(list+i)=*(list+j);
*(list+j)=t;
}
}
}
return list;
}

int *reverse(int *list)
{
int *b;
int i;
b=(int *)malloc(count*sizeof(int ));
for(i=0;i<count;i++)
{
*(b+i)=*(list+count-i-1);
}
list=b;
return list;
}

int *delete(int *list)
{
int x,i,j,num;
x=0;
printf("Enter the element you want to delete");
scanf("%d",&num);
for(i=0;i<count;i++)
{
if(*(list+i)==num)
{
x=i+1;
break;
}
}
for(j=x;j<count;j++)
{
*(list+j-1)=*(list+j);
}
if(x==0)
printf("The element entered by you is not in the list");
count--;
return list;
}

int *pop(int *list)
{
int n,k,result,i,j,c=0;
printf("Enter the index of the value you want to remove");
scanf("%d",&n);
if(n<count)
{
for(i=0;i<count;i++)
{
if(i==n)
{
result=*(list+i);
break;
}
}
k=i+1;
for(j=k;j<count;j++)
{
*(list+j-1)=*(list+j);
}
count--;
printf("The element removed by you is : %d\n",result);
}
else
{
printf("ERROR\\....INDEX OUT OF RANGE Exception");
}
return list;
}

void index(int *list)
{
int n,c,i;
c=-1;
printf("Enter the value of whose index you want to find");
scanf("%d",&n);
if(c!=-1)
{
for(i=0;i<count;i++)
{
if(*(list+i)==n)
{
c=i;
break;
}
}
printf("The index of %d in the list is : %d\n",n,c);
}
else
{
printf("The element entered by you is not in the list");
}
}

void ccount(int *list)
{
int num,i,c=0;
printf("Enter the number whose count you want to find");
scanf("%d",&num);
for(i=0;i<count;i++)
{
if(*(list+i)==num)
{
c=c+1;
}
}
printf("The count of %d in the list is : %d \n",num,c);
}

int *insert(int *list)
{
int *b;
int n,num,i,k,temp;
printf("Enter the number you want to insert in the list : ");
scanf("%d",&num);
printf("Enter the index at which you want to enter the number in the list : ");
scanf("%d",&n);
b=(int *)malloc(count*sizeof(int)); 
k=n;
*(list+k)=num;
for(i=k;i<=count;i++)
{
temp=*(list+i);
*(list+i+1)=temp;
}
count++;
return list;
}

int *extend(int *list)
{
int *b;
int n,num,i,j,k;
b=(int *)malloc((count-1)*sizeof(int));
printf("Enter the number of elements you wnat to enter in the list");
scanf(" %d",&n);

for(i=0;i<count;i++)
{
*(b+i)=*(list+i);
}

list=(int *)malloc((count+n+1)*sizeof(int));

for(j=0;j<count;j++)
{
*(list+j)=*(b+j);
}
for(k=0;k<n;k++)
{
printf("Enter the number you want to add");
scanf("%d",&num);
*(list+count+k)=num;
}

count=num+n;
return list;
}



